package com.example.twotwo_project;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.tow_tow_jo_2.MainActivity.DBReader;

import android.support.v7.app.ActionBarActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Mid_ResearchActivity extends ActionBarActivity {
	
	String URL = "http://192.168.10.103:8888/TowTow_Server/group/group_list.jsp";
	HttpClient client = new DefaultHttpClient();
	DBReader dbread;
	String temp = "";
	String[] data;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		dbread = new DBReader();
		dbread.execute();
	}
	
	public class DBReader extends AsyncTask<Void, String, Void>{

	protected Void doInBackground(Void... params) {
		try{
			HttpGet get = new HttpGet(URL);// 필드에 정의된 URL주소를 가지고 와서 get에 삽입
			HttpResponse resp = client.execute(get);
			BufferedReader br = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
			String str = null;
			StringBuilder sb = new StringBuilder();
			
			while((str = br.readLine()) != null){
				sb.append(str + "\n");
			}br.close();
			
			temp = sb.toString();
			temp = temp.trim(); // 불필요한 공백 제거 함수 trim();
			System.out.println("성공");
			
			data = temp.split("\n"); // 줄바꾸기 문자를 기준으로 문자열을 분리
			System.out.println("여기까지함");
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)*/
	 
		protected void onPostExecute(Void result) { // background 작업 끝나고, UI 작업 수행
			super.onPostExecute(result);
			// 서버로부터 읽은 값을 텍스트뷰에 뿌리기
			TextView tv = (TextView)findViewById(R.id.textView4);
			tv.setText("");
			for(int i=0;i<data.length;i++){
				tv.append(data[i] + "\n");
			}
		}		
	}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.mid_research, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(android.view.MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch(item.getItemId()){
		case android.R.id.home:
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
